<template>
    <el-card class="box-card">
        <el-row>
            <el-alert title="个人信息" type="success" :closable="false" />
        </el-row>
        <el-row>
            <el-col :span="5">
                <p>
                    <!-- <el-avatar :icon="UserFilled" size="large" /> -->
                    <el-avatar class="avatar" :src="avatarUrl"></el-avatar>
                </p>
            </el-col>
            <el-col :span="19">
                <p class="marging">{{ userInfo.name }}</p>
                <p class="marging">{{ moment(userInfo.createTime).format('YYYY-MM-DD') }}</p>
                <p class="marging"><el-button type="warning" plain @click="logout">退出登录</el-button></p>
            </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
            <el-alert title="记忆统计" type="info" :closable="false" />
        </el-row>

        <el-row v-loading="loading">
            <p ref="chartRef" class="chartSize"></p>
        </el-row>

      <el-row v-if="hadFailChart">
          <p ref="chartFaulRef" class="chartSize"></p>
        </el-row>
    </el-card>
</template>

<script setup>
    import { ref, onMounted } from 'vue'
    // import { UserFilled } from '@element-plus/icons-vue'
    import {userProfiles} from '../apis/login'
    import moment from 'moment'
    import * as echarts from 'echarts';
    import { useRouter } from 'vue-router';
    import { useStore } from 'vuex';

    const userInfo = ref({
        name: '',
        createTime: ''
    });

    const statistical = ref({
        "totalWordCount": 0,
        "completeCount": 0,
        "totalReviewCount": 0,
        "failCount": 0
    });

    const avatarUrl = ref('');

    function hash(key) {
        let hashValue = 0;
        for (let i = 0; i < key.length; i++) {
            hashValue = (hashValue << 5) + hashValue + key.charCodeAt(i);
            hashValue &= hashValue; // 保留 32 位整数
        }
        return hashValue;
    }

    const router = useRouter();
    const store = useStore();
    const logout = () => {
        router.push('/login');
        store.commit("logout");
    }


    const chartRef = ref()
    const chartFaulRef = ref()
    const hadFailChart = ref(false);
    const loading = ref(true);

    onMounted(() => {

        userProfiles({}).then((res) => {
            userInfo.value = res.data.custInfo;
            statistical.value = res.data.statistical;
            hadFailChart.value = !!statistical.value.failCountList;
            console.log(hadFailChart.value);

            loading.value = false;

            let i = hash(userInfo.value.name) % 4 + 1;
            // avatarUrl.value = '/src/assets/avatar' + i + '.png'
            avatarUrl.value = '/avatar' + i + '.png'
        }).finally(() => {
            // 基于准备好的dom，初始化echarts实例
            var myChart = echarts.init(chartRef.value);
    
            // 绘制图表
            myChart.setOption({
                title: {
                    text: '记忆统计',
                    subtext: '错误率：' + ((statistical.value.failCount/statistical.value.totalReviewCount)*100).toFixed(2) + "%",
                    left: 'center'
                },
                tooltip: {
                    trigger: 'item'
                },
                legend: {
                    orient: 'vertical',
                    left: 'left'
                },
                series: [
                    {
                    // name: 'Access From',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        { value: statistical.value.totalWordCount, name: '单词总数' },
                        { value: statistical.value.completeCount, name: '完成总数' },
                        { value: statistical.value.totalReviewCount, name: '记忆总数' },
                        { value: statistical.value.failCount, name: '错误次数' },
                        // { value: 300, name: 'Video Ads' }
                    ],
                    emphasis: {
                        itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                    }
                ]
            });

          if(hadFailChart.value) {
            var myChart2 = echarts.init(chartFaulRef.value);
            let data1 = [];
            let data2 = [];
            for(let i = 0; i < statistical.value.failCountList.length; i++) {
              data1[i] = statistical.value.failCountList[i].dateStr.slice(5, 10);
              data2[i] = statistical.value.failCountList[i].num;
            }

            // 绘制图表
            myChart2.setOption({
              title: {
                text: '错误曲线',
                left: 'center'
              },
              xAxis: {
                type: 'category',
                data: data1
              },
              yAxis: {
                type: 'value'
              },
              series: [
                {
                  data: data2,
                  type: 'line'
                }
              ]
            });
          }
        });

    })
</script>

<style scoped>
.marging {
    margin: 10px;
    margin-left: 5px;
}

@media screen and (min-width: 320px) {
    .avatar {
        width: 60px;
        height: 60px;
    }

    .chartSize {
        width: 100%; 
        height: 400px;
    }
  }

  @media screen and (min-width: 600px) {
    .avatar {
        width: 80px;
        height: 80px;
    }

    .chartSize {
        width: 100%; 
        height: 450px;
    }
  }
</style>
  